
#include "game.h"
#include "SDL.h"
#include "data.h"
#include "dpc.h"
#include "wtf.h"

#ifdef __WIN32__
#include <windows.h>
#endif

using namespace WtfEngine;

#define _FATAL_ERROR "\n\nConsult the engine log for technical information.\nIf the problem persists, contact technical support."

#ifdef __WIN32__
#	ifdef __UNICODE__
#		define UniMessageBox MessageBoxW
#		define _FATAL_ERROR_T L"WtfEngine: Fatal Error"
#	else
#		define UniMessageBox MessageBoxA
#		define _FATAL_ERROR_T "WtfEngine: Fatal Error"
#	endif

	static String __gsFatalError;
#	define FATAL_ERROR(sMsg) \
		__gsFatalError = String(sMsg) + _FATAL_ERROR; \
		UniMessageBox(NULL, __gsFatalError.str(), _FATAL_ERROR_T, MB_ICONERROR)

#else
#	define FATAL_ERROR(sMsg) \
	std::cerr << "Fatal Error: " << sMsg << _FATAL_ERROR << std::endl
#endif

void WtfEngine::Start() {
	try{
		//
		// Initialize the kernel
		//
		
		new MemoryManager();
		new Kernel();
		new SystemLog();
		new DataManager();
		new StringPool();
		
		RegisterDataTypes();

		// main loop
		WtfEngine::Kernel::DebugPrint("Initialized, entering main loop");
		WtfEngine::Kernel::GetInstance().RunGameLoop();
	}catch(char* s){
		SDL_Quit();
		FATAL_ERROR(s);
	}catch(String s){
		SDL_Quit();
		FATAL_ERROR(s.str());
	}catch(const std::exception& e){
		SDL_Quit();
		FATAL_ERROR(e.what());
	}
	catch(const std::exception* e){
		SDL_Quit();
		FATAL_ERROR(e->what());
	};
	
	//delete MemoryManager::GetInstancePtr();
};

// ENTRY POINT
extern "C" int main(int argc, char *argv[])
{
	WtfEngine::Start();
	return 0;
};
